/**
 * This file is part of iSecurePasswords.
 *
 * iSecurePasswords is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * iSecurePasswords is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * iSecurePasswords. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Matthew MacGregor Created: 2012 iSecurePasswords, copyright 2013
 * Sudolink, LLC
 *
 */
package com.sudolink.isp.ui.locktimer;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class LockManager implements IManageLocks {

    private final Set<ICanBeLocked> lockables;
    private final ITime lockTimer;

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public LockManager(ITime timer) {
        lockables = new HashSet<>();
        lockTimer = timer;
        IDoSomething listener = lockTimer.getListener();
        lockTimer.setListener(() -> {
            listener.onAction();
            lockAll();
        });
    }

    /**
     * Register an object to be locked when lockAll() is called on the LockManager.
     * @param lockable 
     */
    @Override
    public void register(ICanBeLocked lockable) {
        lockables.add(lockable);
    }

    /**
     * Locks all objects that are currently registered.
     */
    @Override
    public void lockAll() {
        lockables.stream().forEach((frame) -> {
            frame.lock();
        });
        lockTimer.stop();
    }

    /**
     * Unlocks all objects that are currently registered. If an object is not
     * available, meaning that isAvailable() is false, then that object will be
     * removed from the list.
     */
    @Override
    public void unlockAll() {

        Iterator<ICanBeLocked> iter = lockables.iterator();
        ICanBeLocked lockable;
        while (iter.hasNext()) {
            lockable = iter.next();
            if (lockable.isAvailable()) {
                lockable.unlock();
            } else {
                iter.remove();
            }
        }
        lockTimer.start();

    }

    /**
     * Unregister an object for locking.
     * @param lockable The item to be unregistered.
     */
    @Override
    public void unregister(ICanBeLocked lockable) {
        lockables.remove(lockable);
    }

    /**
     * Start the lock manager, which in turn starts the lock timer. If the timer
     * is already started, this method has the same effect as restart().
     */
    @Override
    public void start() {

        lockTimer.start();

    }

    /**
     * Restart the lock manager. This resets the timer.
     */
    @Override
    public void restart() {
        lockTimer.start();
    }


}
